package com.fancy.mobileschool.ui.adapt;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fancy.mobileschool.R;
import com.fancy.mobileschool.entity.YunFile;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jackie on 2017/11/16.
 */

public class TransferFileListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;

    private static final int ALREADY_UPLOAD_FILE = 1;

    private static final int UPLOADING_FILE = 2;

    private static final int DOWNLOAD_FILE = 3;

    private static final int UPLOADING_TITLE = 4;

    private static final int ALREADY_UPLOAD_TITLE = 5;

    private static final int DOWNLOAD_TITLE = 6;

    private int uploadingFileGroupTilePosition = 0;

    private volatile int alreadyUploadFileGroupTitlePosition;

    private volatile int downloadFileGroupTitlePosition;

    private List<YunFile> uploadingFiles;

    private List<YunFile> alreadyUploadFiles;

    private List<YunFile> downloadFiles;



    public TransferFileListAdapter(List<YunFile> uploadingFiles, List<YunFile> alreadyUploadFiles, List<YunFile> downloadFiles, Context context) {
        this.uploadingFiles = uploadingFiles;
        this.alreadyUploadFiles = alreadyUploadFiles;
        this.downloadFiles = downloadFiles;
        this.context = context;

        alreadyUploadFileGroupTitlePosition = uploadingFiles.size() + 1;
        downloadFileGroupTitlePosition = uploadingFiles.size() + alreadyUploadFiles.size() + 2;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == UPLOADING_TITLE){
            View view = LayoutInflater.from(context).inflate(R.layout.file_group_title, parent, false);
            TextView textView = (TextView) view.findViewById(R.id.file_group_title_textview);
            textView.setText("正在上传");
            return new GroupViewHolder(view);
        }else if (viewType == DOWNLOAD_TITLE){
            View view = LayoutInflater.from(context).inflate(R.layout.file_group_title, parent, false);
            TextView textView = (TextView) view.findViewById(R.id.file_group_title_textview);
            textView.setText("下载完成");
            return new GroupViewHolder(view);
        }else if (viewType == ALREADY_UPLOAD_TITLE) {
            View view = LayoutInflater.from(context).inflate(R.layout.file_group_title, parent, false);
            TextView textView = (TextView) view.findViewById(R.id.file_group_title_textview);
            textView.setText("上传完成");
            return new GroupViewHolder(view);
        }else {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.fragment_file, parent, false);
            return new FileViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == UPLOADING_FILE) {
            FileViewHolder fileViewHolder = (FileViewHolder) holder;
            fileViewHolder.file_item_name.setText(uploadingFiles.get(position - 1).file_name);
        }else if (getItemViewType(position) == ALREADY_UPLOAD_FILE) {
            FileViewHolder fileViewHolder = (FileViewHolder) holder;
            fileViewHolder.file_item_name.setText(alreadyUploadFiles.get(position - uploadingFiles.size() - 2).file_name);
        }else if (getItemViewType(position) == DOWNLOAD_FILE) {
            FileViewHolder fileViewHolder = (FileViewHolder) holder;
            fileViewHolder.file_item_name.setText(downloadFiles.get(position - uploadingFiles.size() - alreadyUploadFiles.size() - 3).file_name);
        }
    }

    @Override
    public int getItemCount() {
        return uploadingFiles.size() + alreadyUploadFiles.size() + downloadFiles.size() + 3;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == uploadingFileGroupTilePosition)
            return UPLOADING_TITLE;
        else if (position < alreadyUploadFileGroupTitlePosition)
            return UPLOADING_FILE;
        else if (position == alreadyUploadFileGroupTitlePosition)
            return ALREADY_UPLOAD_TITLE;
        else if (position < downloadFileGroupTitlePosition)
            return ALREADY_UPLOAD_FILE;
        else if (position == downloadFileGroupTitlePosition)
            return DOWNLOAD_TITLE;
        else
            return DOWNLOAD_FILE;
    }


    public TransferFileListAdapter addFile2UploadingFiles(YunFile yunFile) {
        uploadingFiles.add(yunFile);
        alreadyUploadFileGroupTitlePosition += 1;
        downloadFileGroupTitlePosition += 1;
        return this;
    }

    public TransferFileListAdapter removeFromUploadingFiles(YunFile yunFile) {
        uploadingFiles.remove(yunFile);
        alreadyUploadFileGroupTitlePosition -= 1;
        downloadFileGroupTitlePosition -= 1;
        return this;
    }

    public TransferFileListAdapter addFile2AlreadyUploadFiles(YunFile yunFile) {
        alreadyUploadFiles.add(yunFile);
        downloadFileGroupTitlePosition += 1;
        return this;
    }

    class GroupViewHolder extends RecyclerView.ViewHolder {

        public GroupViewHolder(View itemView) {
            super(itemView);
        }
    }

    public class FileViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView file_item_name;
        public final TextView file_item_length;
        public YunFile mItem;

        public FileViewHolder(View view) {
            super(view);
            mView = view;
            file_item_name = (TextView) view.findViewById(R.id.file_item_name);
            file_item_length = (TextView) view.findViewById(R.id.file_item_length);
        }
    }

}
